/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of the Maxsi Library.

	Maxsi Library is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi Library is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE. See the GNU LesserGTK General Public
	License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Library. If not, see <http://www.gnu.org/licenses/>.

	Maxsi LibraryGTK
	A powerful Cross-Platform C++ General Purpose Library that allows you to
	efficiently create high-performance and reliable applications.

	MaxsiWindowW32.cpp
	Implements the MaxsiWindowSystem using the W32 platform.

******************************************************************************/

#include "MaxsiLibrary.h"

#ifdef Maxsi_Support_W32

#include <windowsx.h>
#include "commctrl.h"

BeginMaxsiNamespace

int Debug(char* String)
{
	WriteFileSimple(_MESTR("Debug.txt"), (BYTE*)String, strlen(String));

	return 1;
}

int CriticalMsgBox(MaxsiHandle Parent, int Flags, MESTR* ErrorMessage, MESTR* ErrorTitle)
{
	return MessageBoxM((HWND)Parent, ErrorMessage, ErrorTitle, MB_OK | MB_ICONEXCLAMATION);
}


MaxsiWindow* HWND2MaxsiWindow(HWND Window)
{
	MaxsiWindow*	TMP		=	NULL;

	MaxsiWindowSystem*	WindowSystem	=	(MaxsiWindowSystem*)CreateInterface("MaxsiWindowSystem");

	TMP		=	WindowSystem->WindowFirstChild;

	while ( TMP != NULL )
	{
		if ( (HWND)(TMP->Window) == Window ) { return TMP; }

		// Locate the next window!
		while ( true )
		{
			if ( TMP->WindowFirstChild != NULL ) { TMP = TMP->WindowFirstChild; break; } else
			if ( TMP->WindowNext != NULL ) { TMP = TMP->WindowNext; break; } else
			if ( TMP->WindowParent ) { TMP = TMP->WindowParent; }

			return NULL; // Last one!
		}
	}

	return NULL;
}

LRESULT CALLBACK MaxsiWindowSystemW32Proc(
    HWND hwnd,        // handle to window
    UINT uMsg,        // message identifier
    WPARAM wParam,    // first message parameter
    LPARAM lParam)    // second message parameter
{ 
	switch (uMsg) 
    { 
		case WM_CREATE: 
		{ 
			// Initialize the window. 
            return 0; 
		}
		case WM_PAINT: 
		{
			PAINTSTRUCT PaintStructure;
			
			BeginPaint(hwnd, &PaintStructure);

			EndPaint(hwnd, &PaintStructure);

            return 0; 
		}
		case WM_SIZE:
		{
            // Set the size and position of the window. 
            return 0; 
		}
		case WM_DESTROY: 
		{        
			MaxsiWindow*	Window		=	HWND2MaxsiWindow((HWND)hwnd);

			if ( Window != NULL )
			{
				MaxsiWindowEvent	Event;
	
				Event.Owner		=	Window;
				Event.Type		=	MAXSI_EVENT_WINDOW_DESTROYED;

				Window->OnEvent(&Event);
			}

            return 0;
		}
		case WM_COMMAND:
		{
			if ( HIWORD(wParam) == BN_CLICKED )
			{
				MaxsiWindow*	Window		=	HWND2MaxsiWindow((HWND)lParam);

				if ( Window != NULL )
				{
					MaxsiWindowEvent	Event;
		
					Event.Owner		=	Window;
					Event.Type		=	MAXSI_EVENT_PRESSED;
					Event.LParam	=	0;
					Event.RParam	=	0;

					Window->OnEvent(&Event);
				}
			}
			return 0;
		}
		case WM_LBUTTONDOWN:
		{
			SetForegroundWindow(hwnd);
			return 0;
		}

        // 
        // Process other messages. 
        // 
 
        default:
            return DefWindowProcM(hwnd, uMsg, wParam, lParam); 
    } 
    return 0; 
}


//=============================================================================
//	Implement MaxsiWindowSystem as MaxsiWindowSystemW32.
//=============================================================================

MAXSI_DEFINE_IMPLEMENTATION(MaxsiWindowSystemW32, MaxsiWindowSystem, MAXSI_INTERFACE_STATIC);

MaxsiWindowSystemW32::MaxsiWindowSystemW32()
{
	DefaultFont				=	NULL;
	DefaultFontBold			=	NULL;
	DefaultFontLabel		=	NULL;
	DefaultFontButton		=	NULL;
	WindowFirstChild		=	NULL;
}

MaxsiWindowSystemW32::~MaxsiWindowSystemW32()
{
	if ( DefaultFont != NULL ) { DeleteObject(DefaultFont); }
	if ( DefaultFontBold != NULL ) { DeleteObject(DefaultFont); }
	if ( DefaultFontLabel != NULL ) { DeleteObject(DefaultFontLabel); }
	if ( DefaultFontButton != NULL ) { DeleteObject(DefaultFontButton); }
}

int MaxsiWindowSystemW32::Initialize(int* argc, char** argv[])
{
	INITCOMMONCONTROLSEX CommonControlsInitStruct;

	CommonControlsInitStruct.dwSize		=	sizeof(CommonControlsInitStruct);
	CommonControlsInitStruct.dwICC		=	ICC_PROGRESS_CLASS;

	if ( InitCommonControlsEx(&CommonControlsInitStruct) == FALSE ) { MAXSI_TODO("Add error handling!"); }

	if ( DefaultFont != NULL ) { DeleteObject(DefaultFont); }
	if ( DefaultFontBold != NULL ) { DeleteObject(DefaultFont); }
	if ( DefaultFontLabel != NULL ) { DeleteObject(DefaultFontLabel); }
	if ( DefaultFontButton != NULL ) { DeleteObject(DefaultFontButton); }

	DefaultFont			=	CreateFontM(13, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
		OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
		DEFAULT_PITCH | FF_SWISS, _MESTR("Verdana"));

	DefaultFontBold		=	CreateFontM(13, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET,
		OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
		DEFAULT_PITCH | FF_SWISS, _MESTR("Verdana"));

	DefaultFontLabel	=	CreateFontM(13, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
		OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
		DEFAULT_PITCH | FF_SWISS, _MESTR("Verdana"));

	DefaultFontButton	=	CreateFontM(10, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
		OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
		DEFAULT_PITCH | FF_SWISS, _MESTR("MS Sans Serif"));

	WNDCLASSW  WindowClass = {0};

	WindowClass.lpfnWndProc = (WNDPROC)MaxsiWindowSystemW32Proc;
	WindowClass.hInstance = (HINSTANCE)GetModuleHandle(NULL);
	WindowClass.hIcon = (HICON)LoadImageM(NULL, _MESTR("../Images/MaxsiIcon.ico"), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
	//WindowClass.hIcon = LoadIconM((HINSTANCE)hInstance, _MESTR("ProductIcon"));
	WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	WindowClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
	WindowClass.lpszClassName = _MESTR("MaxsiWindowSystemW32Window");

	if (!RegisterClassM(&WindowClass)) { return FALSE; }

	return 1;
}

void MaxsiWindowSystemW32::ShutDown()
{
	PostQuitMessage(0);
}

int MaxsiWindowSystemW32::MainLoop()
{
	MSG		msg			=	{0};
	BOOL	bRet		=	FALSE;

	// Begin the mainloop
	while( (bRet = GetMessageM( &msg, NULL, 0, 0 )) != 0)
	{ 
		if ( bRet == -1 ) { break; }

		TranslateMessage(&msg);
		DispatchMessageM(&msg); 
	}

	return (int)msg.wParam;
}

MaxsiWindow* MaxsiWindowSystemW32::CreateWindow(MaxsiWindow* Parent, MESTR* ClassName, int Width, int Height, int Left, int Top, MESTR* Title)
{
	MaxsiWindow*	Result	=	NULL;

	if ( MESTRCMP(ClassName, _MESTR("Window")) == 0 ) { Result = new MaxsiWindowW32; } else
	if ( MESTRCMP(ClassName, _MESTR("Button")) == 0 ) { Result = new MaxsiButtonW32; } else
	if ( MESTRCMP(ClassName, _MESTR("ProgressBar")) == 0 ) { Result = new MaxsiProgressBarW32; } else
	{ PrintOutput("Attempted to create unknown control type!\n"); }

	if ( Result == NULL )	
	{
		delete Result;
		return NULL;
	}

	Result->WindowSystem = this;

	if ( Parent != NULL )
	{
		Result->WindowNext			=	Parent->WindowFirstChild;
		Parent->WindowFirstChild	=	Result;	
	}
	else
	{
		Result->WindowNext			=	this->WindowFirstChild;
		this->WindowFirstChild			=	Result;	
	}

 	if ( Result->Initialize(Parent, ClassName, Width, Height, Left, Top, Title) != MAXSI_ERROR_SUCCESS )
	{
		if ( Parent != NULL )
		{
			Parent->WindowFirstChild	=	Result->WindowNext;
		}
		else
		{
			this->WindowFirstChild		=	Result->WindowNext;
		}

		delete Result;
		return NULL;	
	}
	
	return Result;
}

void MaxsiWindowSystemW32::DeleteWindow(MaxsiWindow* Window)
{
	if ( Window != NULL )
	{
		Window->Destroy();
		delete Window;
	}	
}

int MaxsiWindowSystemW32::MsgBox(MaxsiHandle Parent, int Flags, MESTR* Message, MESTR* Title)
{
	return MessageBoxM((HWND)Parent, Message, Title, MB_OK | MB_ICONEXCLAMATION);
}

//=============================================================================
//	Implement MaxsiWindow as MaxsiWindowW32.
//=============================================================================

MAXSI_DEFINE_IMPLEMENTATION(MaxsiWindowW32, MaxsiWindow, MAXSI_INTERFACE_DYNAMIC);

MaxsiWindowW32::MaxsiWindowW32()
{
	Text					=	NULL;
	WindowFirstChild		=	NULL;
	WindowNext				=	NULL;
	CurrentProgress			=	0.0f;
	CurrentProgressType		=	0;
}

MaxsiWindowW32::~MaxsiWindowW32()
{
	IFDEL(Text);
}

MaxsiError MaxsiWindowW32::Initialize(MaxsiWindow* Parent, MESTR* ClassName, int Width, int Height, int Left, int Top, MESTR* Title)
{
	WindowLeft		=	Left;
	WindowTop		=	Top;
	WindowWidth		=	Width;
	WindowHeight	=	Height;

	DWORD	Flags	=	0;

	if ( Parent == NULL ) { Flags = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX; }
	if ( Parent != NULL ) { Flags = WS_CHILDWINDOW | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX; }

	Window = (MaxsiHandle)CreateWindowM(
        _MESTR("MaxsiWindowSystemW32Window"),
        Title,
        Flags,
        Left, Top,
        Width, Height,
        (Parent) ? (HWND)Parent->Window : NULL,
		NULL, GetModuleHandle(NULL), NULL );

	if ( Window == NULL ) { return MAXSI_ERROR_COULD_NOT_CREATE_WINDOW; }

	return MAXSI_ERROR_SUCCESS;	
}

MaxsiError MaxsiWindowW32::Destroy()
{
	DestroyWindow((HWND)Window);

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError MaxsiWindowW32::Become(MaxsiWindow* NewSelf)
{
	NewSelf->SetText(Text);
	NewSelf->SetProgress(CurrentProgress, CurrentProgressType);
	NewSelf->Move(Left(), Top(), Width(), Height());

	return MAXSI_ERROR_SUCCESS;	
}

MaxsiError MaxsiWindowW32::OnEvent(MaxsiWindowEvent* Event)
{
	MaxsiError	Result	=	BaseClass::OnEvent(Event);

	if ( Result != MAXSI_ERROR_UNHANDLED_EVENT ) { return Result; }

	if ( EventHandler ) { return EventHandler(Event); } else { return MAXSI_ERROR_SUCCESS; }
}

MaxsiError MaxsiWindowW32::Show()
{
	ShowWindow((HWND)Window, SW_SHOW);

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError MaxsiWindowW32::Hide()
{
	ShowWindow((HWND)Window, SW_HIDE);

	return MAXSI_ERROR_SUCCESS;
}

void MaxsiWindowW32::SetEventHandler(MaxsiWindowEventHandler Handler)
{
	EventHandler	=	Handler;
}

MaxsiWindow* MaxsiWindowW32::CreateChild(MESTR* ClassName, int Width, int Height, int Left, int Top, MESTR* Title)
{
	return WindowSystem->CreateWindow(this, ClassName, Width, Height, Left, Top, Title);
}

MESTR* MaxsiWindowW32::GetControlType()
{
	return MAXSI_ERROR_SUCCESS;
}

int MaxsiWindowW32::Left()
{
	return WindowLeft;
}

int MaxsiWindowW32::Top()
{
	return WindowTop;	
}

int MaxsiWindowW32::Width()
{
	return WindowWidth;
}

int MaxsiWindowW32::Height()
{
	return WindowHeight;
}

int MaxsiWindowW32::Right()
{
	return Left() + Width();
}

int MaxsiWindowW32::Bottom()
{
	return Top() + Height();
}

MaxsiError MaxsiWindowW32::Move(int NewLeft, int NewTop, int NewWidth, int NewHeight)
{
	MoveWindow((HWND)Window, WindowLeft, WindowTop, WindowWidth, WindowHeight, TRUE);

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError MaxsiWindowW32::SetText(MESTR* NewText)
{
	MESTR*	TextCopy	=	BuildString(1, NewText);
	
	if ( TextCopy != NULL )
	{
		if ( Text != NULL ) { delete[] Text; }

		Text = TextCopy;
		
	}

	SetWindowTextM((HWND)Window, NewText);

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError MaxsiWindowW32::SetFont(MaxsiHandle NewFont)
{
	return MAXSI_ERROR_SUCCESS;
}

MaxsiError MaxsiWindowW32::SetProgress(float Progress, int Type)
{
	CurrentProgress			=	Progress;
	CurrentProgressType		=	Type;

	if ( Type == MAXSI_PROGRESS_NONE )
	{
		Win7TaskBarSetState((HWND)Window, TBPF_NOPROGRESS);
	}
	else if ( Type == MAXSI_PROGRESS_NORMAL )
	{
		Win7TaskBarSetState((HWND)Window, TBPF_NORMAL);
		Win7TaskBarSetProgress((HWND)Window, (ULONGLONG)(Progress*100.0f), 100);
	}
	else if ( Type == MAXSI_PROGRESS_INDETERMINATE )
	{
		Win7TaskBarSetState((HWND)Window, TBPF_INDETERMINATE);
		Win7TaskBarSetProgress((HWND)Window, 100, 100);
	}
	else if ( Type == MAXSI_PROGRESS_ERROR )
	{
		Win7TaskBarSetState((HWND)Window, TBPF_ERROR);
		Win7TaskBarSetProgress((HWND)Window, 100, 100);
	}
	else if ( Type == MAXSI_PROGRESS_PAUSED )
	{
		Win7TaskBarSetState((HWND)Window, TBPF_PAUSED);
		Win7TaskBarSetProgress((HWND)Window, 100, 100);
	}
	else
	{
		return MAXSI_ERROR_NOT_SUPPORTED;
	}

	return MAXSI_ERROR_SUCCESS;
}

MESTR* MaxsiWindowW32::GetText()
{
	return BuildString(1, Text);
}

//=============================================================================
//	Implement MaxsiWindow as MaxsiBaseControlW32.
//=============================================================================

MaxsiBaseControlW32::MaxsiBaseControlW32()
{
	Text					=	NULL;
	WindowFirstChild		=	NULL;
	WindowNext				=	NULL;
	CurrentProgress			=	0.0f;
	CurrentProgressType		=	0;
}

MaxsiBaseControlW32::~MaxsiBaseControlW32()
{
	IFDEL(Text);
}

MaxsiError MaxsiBaseControlW32::Destroy()
{
	DestroyWindow((HWND)Window);

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError MaxsiBaseControlW32::OnEvent(MaxsiWindowEvent* Event)
{
	MaxsiError	Result	=	BaseClass::OnEvent(Event);

	if ( Result != MAXSI_ERROR_UNHANDLED_EVENT ) { return Result; }

	if ( WindowParent == NULL ) { return MAXSI_ERROR_NOT_INITIALIZED; }
	return WindowParent->OnEvent(Event);	
}

MaxsiError MaxsiBaseControlW32::Show()
{
	ShowWindow((HWND)Window, SW_SHOW);	

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError MaxsiBaseControlW32::Hide()
{
	ShowWindow((HWND)Window, SW_HIDE);

	return MAXSI_ERROR_SUCCESS;
}

void MaxsiBaseControlW32::SetEventHandler(MaxsiWindowEventHandler Handler)
{
	EventHandler	=	Handler;
}

MaxsiWindow* MaxsiBaseControlW32::CreateChild(MESTR* ClassName, int Width, int Height, int Left, int Top, MESTR* Title)
{
	return NULL;
}

int MaxsiBaseControlW32::Left()
{
	return WindowLeft;
}

int MaxsiBaseControlW32::Top()
{
	return WindowTop;	
}

int MaxsiBaseControlW32::Width()
{
	return WindowWidth;
}

int MaxsiBaseControlW32::Height()
{
	return WindowHeight;
}

int MaxsiBaseControlW32::Right()
{
	return Left() + Width();
}

int MaxsiBaseControlW32::Bottom()
{
	return Top() + Height();
}

MaxsiError MaxsiBaseControlW32::Move(int NewLeft, int NewTop, int NewWidth, int NewHeight)
{
	WindowLeft		=	NewLeft;
	WindowTop		=	NewTop;
	WindowWidth		=	NewWidth;
	WindowHeight	=	NewHeight;

	MoveWindow((HWND)Window, WindowLeft, WindowTop, WindowWidth, WindowHeight, TRUE);

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError MaxsiBaseControlW32::SetProgress(float Progress, int Type)
{
	return MAXSI_ERROR_NOT_SUPPORTED;
}

MaxsiError MaxsiBaseControlW32::SetText(MESTR* NewText)
{
	return MAXSI_ERROR_NOT_SUPPORTED;
}

MaxsiError MaxsiBaseControlW32::SetFont(MaxsiHandle NewFont)
{
	return MAXSI_ERROR_NOT_SUPPORTED;
}

MESTR* MaxsiBaseControlW32::GetText()
{
	return BuildString(1, Text);
}

//=============================================================================
//	Implement MaxsiWindow as MaxsiButtonW32.
//=============================================================================

MAXSI_DEFINE_IMPLEMENTATION(MaxsiButtonW32, MaxsiWindow, MAXSI_INTERFACE_DYNAMIC);

MaxsiButtonW32::MaxsiButtonW32()
{

}

MaxsiButtonW32::~MaxsiButtonW32()
{

}

MaxsiError MaxsiButtonW32::Initialize(MaxsiWindow* Parent, MESTR* ClassName, int Width, int Height, int Left, int Top, MESTR* Title)
{
	Window = (MaxsiHandle)CreateWindowM(
        _MESTR("BUTTON"),
        Title,
        WS_TABSTOP | WS_VISIBLE | WS_CHILD,
        Left, Top,
        Width, Height,
        (Parent) ? (HWND)Parent->Window : NULL,
		NULL, GetModuleHandle(NULL), NULL );

	if ( Window == NULL ) { return MAXSI_ERROR_COULD_NOT_CREATE_WINDOW; }

	SetText(Title);

	WindowParent	=	Parent;
	WindowLeft		=	Left;
	WindowTop		=	Top;
	WindowWidth		=	Width;
	WindowHeight	=	Height;

	MaxsiWindowSystemW32*	WindowSystemW32		=	dynamic_cast<MaxsiWindowSystemW32*>(WindowSystem);

	if ( WindowSystemW32 != NULL )
	{
		SendMessageM((HWND)WindowSystemW32, WM_SETFONT, (WPARAM)WindowSystemW32->DefaultFontButton, TRUE);
	}

	return MAXSI_ERROR_SUCCESS;	
}


MaxsiError MaxsiButtonW32::Become(MaxsiWindow* NewSelf)
{
	NewSelf->SetText(Text);
	NewSelf->Move(Left(), Top(), Width(), Height());

	return MAXSI_ERROR_SUCCESS;	
}

MaxsiError MaxsiButtonW32::SetText(MESTR* NewText)
{
	MESTR*	TextCopy	=	BuildString(1, NewText);
	
	if ( TextCopy != NULL )
	{
		if ( Text != NULL ) { delete[] Text; }

		Text = TextCopy;
	}

	SetWindowTextM((HWND)Window, NewText);

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError MaxsiButtonW32::SetFont(MaxsiHandle NewFont)
{
	return MAXSI_ERROR_SUCCESS;
}

MESTR* MaxsiButtonW32::GetControlType()
{
	return _MESTR("Button");
}

//=============================================================================
//	Implement MaxsiWindow as MaxsiProgressBarW32.
//=============================================================================

MAXSI_DEFINE_IMPLEMENTATION(MaxsiProgressBarW32, MaxsiWindow, MAXSI_INTERFACE_DYNAMIC);

MaxsiProgressBarW32::MaxsiProgressBarW32()
{

}

MaxsiProgressBarW32::~MaxsiProgressBarW32()
{

}

MaxsiError MaxsiProgressBarW32::Initialize(MaxsiWindow* Parent, MESTR* ClassName, int Width, int Height, int Left, int Top, MESTR* Title)
{
	Window = (MaxsiHandle)CreateWindowM(
        _MESTR("msctls_progress32"),
        NULL,
        WS_VISIBLE | WS_CHILD,
        Left, Top,
        Width, Height,
        (Parent) ? (HWND)Parent->Window : NULL,
		NULL, GetModuleHandle(NULL), NULL );

	if ( Window == NULL ) { return MAXSI_ERROR_COULD_NOT_CREATE_WINDOW; }

	WindowParent	=	Parent;
	WindowLeft		=	Left;
	WindowTop		=	Top;
	WindowWidth		=	Width;
	WindowHeight	=	Height;

	return MAXSI_ERROR_SUCCESS;	
}


MaxsiError MaxsiProgressBarW32::Become(MaxsiWindow* NewSelf)
{
	NewSelf->SetProgress(CurrentProgress, CurrentProgressType);
	NewSelf->Move(Left(), Top(), Width(), Height());

	return MAXSI_ERROR_SUCCESS;	
}

MaxsiError MaxsiProgressBarW32::SetProgress(float Progress, int Type)
{
	CurrentProgress			=	Progress;
	CurrentProgressType		=	Type;

	if ( Type == MAXSI_PROGRESS_CURRENT )
	{
		SendMessageM((HWND)Window, PBM_SETPOS, (int)(Progress*100.0f), 0);
	}
	else if ( Type == MAXSI_PROGRESS_NORMAL )
	{
		SetWindowLongPtr((HWND)Window, GWL_STYLE, WS_VISIBLE | WS_CHILD | PBS_SMOOTH);
		SendMessageM((HWND)Window, PBM_SETMARQUEE, FALSE, 0);
		SendMessageM((HWND)Window, PBM_SETSTATE, PBST_NORMAL, 0);
		SendMessageM((HWND)Window, PBM_SETPOS,(WPARAM)(int)(Progress*100.0f),(LPARAM)0);
	}
	else if ( Type == MAXSI_PROGRESS_INDETERMINATE )
	{
		SetWindowLongPtr((HWND)Window, GWL_STYLE, WS_VISIBLE | WS_CHILD | PBS_SMOOTH | PBS_MARQUEE);
		SendMessageM((HWND)Window, PBM_SETSTATE, PBST_NORMAL, 0);
		SendMessageM((HWND)Window, PBM_SETMARQUEE, TRUE, 0);
		SendMessageM((HWND)Window, PBM_SETPOS, (int)(Progress*100.0f), 0);
	}
	else if ( Type == MAXSI_PROGRESS_ERROR )
	{
		SetWindowLongPtr((HWND)Window, GWL_STYLE, WS_VISIBLE | WS_CHILD | PBS_SMOOTH);
		SendMessageM((HWND)Window, PBM_SETMARQUEE, FALSE, 0);
		SendMessageM((HWND)Window, PBM_SETPOS,(WPARAM)100, (LPARAM)0);
		SendMessageM((HWND)Window, PBM_SETSTATE, PBST_ERROR, 0);
	}
	else if ( Type == MAXSI_PROGRESS_PAUSED )
	{
		SetWindowLongPtr((HWND)Window, GWL_STYLE, WS_VISIBLE | WS_CHILD | PBS_SMOOTH);
		SendMessageM((HWND)Window, PBM_SETMARQUEE, FALSE, 0);
		SendMessageM((HWND)Window, PBM_SETPOS,(WPARAM)100, (LPARAM)0);
		SendMessageM((HWND)Window, PBM_SETSTATE, PBST_PAUSED, 0);
	}
	else
	{
		return false;
	}

	return MAXSI_ERROR_SUCCESS;
}

MESTR* MaxsiProgressBarW32::GetControlType()
{
	return _MESTR("ProgressBar");
}

EndMaxsiNamespace

#endif

